﻿using Swashbuckle.AspNetCore.SwaggerGen;
using System.Linq;
using Microsoft.OpenApi.Models;

namespace zijian666.WebApi.SwaggerOptions
{
    class SupportMultipleMethodsFilter : IDocumentFilter
    {
        public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
        {
            // 只要一个请求支持多个Method 且包含POST方法, 在文档中就仅展示POST
            foreach (var path in swaggerDoc.Paths)
            {
                var keys = path.Value.Operations.Keys.Where(x => x != OperationType.Post).ToList();
                if (keys.Count < path.Value.Operations.Count)
                {
                    // 只保留POST请求
                    keys.ForEach(x => path.Value.Operations.Remove(x));
                }
            }

        }
    }
}
